package boardgamer.yavalath.client.resources;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Typeface;
import boardgamer.yavalath.R;
import com.google.common.collect.ImmutableMap;

import java.util.Map;

public class ImageHandler {
  public static Resources resources;
  public static Bitmap tile;
  public static Bitmap tileVertical;
  public static Bitmap tileGlow;
  public static Bitmap tileVerticalGlow;
  public static Bitmap menuGoButton;
  public static Bitmap share;
  public static Bitmap soundOn;
  public static Bitmap soundOff;
  public static Bitmap playerAddButton;
  public static Bitmap playerRemoveButton;
  public static Bitmap cutBigButton;
  public static Bitmap cutBigButtonGlow;
  public static Bitmap cutBigButtonDisabled;
  public static Bitmap lock;

  public static Bitmap aiButton;
  public static Bitmap humanButton;
  public static Bitmap closedButton;
  public static Bitmap playerModeGlow;

  public static Bitmap colorPicker;
  public static Bitmap colorPickerGlow;

  public static Bitmap headerBackground;
  public static Bitmap playerNameBackground;
  public static Bitmap playerNameBackgroundGlow;
  
  public static Bitmap backgroundHex1;
  public static Bitmap backgroundHex2;
  public static Bitmap backgroundHex3;
  public static Bitmap backgroundHex4;
  public static Bitmap backgroundHex5;

  public static Typeface headingFont;
  public static Typeface normalFont;
  public static Map<Typeface, RectF> boundSizeRatio;

  public static void initialize(Context context, FontHandler fontHandler) {
    resources = context.getResources();
    initializeGraphics();
    initializeTypefaces(fontHandler);
  }

  private static void initializeGraphics() {
    tile = BitmapFactory.decodeResource(resources, R.drawable.glass_tile);
    tileVertical = BitmapFactory.decodeResource(resources, R.drawable.vertical_glass_tile);
    tileGlow = BitmapFactory.decodeResource(resources, R.drawable.tile_glow);
    tileVerticalGlow = BitmapFactory.decodeResource(resources, R.drawable.vertical_tile_glow);
    menuGoButton = BitmapFactory.decodeResource(resources, R.drawable.go_button);
    share = BitmapFactory.decodeResource(resources, R.drawable.share_button);
    soundOn = BitmapFactory.decodeResource(resources, R.drawable.mute_button_unmute);
    soundOff = BitmapFactory.decodeResource(resources, R.drawable.mute_button_mute);
    playerAddButton = BitmapFactory.decodeResource(resources, R.drawable.player_add_button);
    playerRemoveButton = BitmapFactory.decodeResource(resources, R.drawable.player_remove_button);
    cutBigButton = BitmapFactory.decodeResource(resources, R.drawable.cut_big_button);
    cutBigButtonGlow = BitmapFactory.decodeResource(resources, R.drawable.cut_big_button_glow);
    cutBigButtonDisabled
        = BitmapFactory.decodeResource(resources, R.drawable.cut_big_button_disabled);
    lock = BitmapFactory.decodeResource(resources, R.drawable.lock);

    aiButton = BitmapFactory.decodeResource(resources, R.drawable.ai_button);
    humanButton = BitmapFactory.decodeResource(resources, R.drawable.human_button);
    closedButton = BitmapFactory.decodeResource(resources, R.drawable.closed_button);
    playerModeGlow = BitmapFactory.decodeResource(resources, R.drawable.player_mode_glow);

    colorPicker = BitmapFactory.decodeResource(resources, R.drawable.color_picker);
    colorPickerGlow = BitmapFactory.decodeResource(resources, R.drawable.color_picker_glow);

    headerBackground = BitmapFactory.decodeResource(resources, R.drawable.header_background);
    playerNameBackground
        = BitmapFactory.decodeResource(resources, R.drawable.player_name_background);
    playerNameBackgroundGlow
        = BitmapFactory.decodeResource(resources, R.drawable.player_name_background_glow);

    backgroundHex1 = BitmapFactory.decodeResource(resources, R.drawable.background_hex_1);
    backgroundHex2 = BitmapFactory.decodeResource(resources, R.drawable.background_hex_2);
    backgroundHex3 = BitmapFactory.decodeResource(resources, R.drawable.background_hex_3);
    backgroundHex4 = BitmapFactory.decodeResource(resources, R.drawable.background_hex_4);
    backgroundHex5 = BitmapFactory.decodeResource(resources, R.drawable.background_hex_5);
  }

  private static void initializeTypefaces(FontHandler fontHandler) {
    headingFont = fontHandler.getHeadingFont();
    normalFont = fontHandler.getNormalFont();

    ImmutableMap.Builder<Typeface, RectF> boundSizeRatioBuilder = ImmutableMap.builder();
    boundSizeRatioBuilder.put(headingFont, getMaxBoundToSizeRatio(headingFont));
    boundSizeRatioBuilder.put(normalFont, getMaxBoundToSizeRatio(normalFont));
    boundSizeRatio = boundSizeRatioBuilder.build();
  }

  public static RectF getMaxBoundToSizeRatio(Typeface font) {
    float size = 100;
    Rect bound = new Rect();
    Paint paint = new Paint();
    paint.setTypeface(font);
    paint.setTextSize(size);
    for (char c = 0; c < 128; c++) {
      Rect temp = new Rect();
      paint.getTextBounds(String.valueOf(c), 0, 1, temp);
      bound.union(temp);
    }

    return new RectF(0, 0, bound.width() / size, bound.height() / size);
  }
}